<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
          Google Web Toolkit
          
            -
            Static String Internationalization</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link href="../css/base.css" rel="stylesheet" type="text/css">
<link href="./doc.css" rel="stylesheet" type="text/css">
<link href="../css/print.css" rel="stylesheet" media="print" type="text/css">
</head>
<body>
<div id="gaia">&nbsp;</div>
<div id="header">
<div id="logo">
<a href="http://code.google.com/"><img src="http://code.google.com/images/code_sm.png" alt="Google"></a>
</div>
<div id="title">Google Web Toolkit (Beta)</div>
<div id="breadcrumbs">
<div id="nextprev">
<nobr><a href="com.google.gwt.doc.DeveloperGuide.Internationalization.html">&laquo; prev</a></nobr><nobr><a href="com.google.gwt.doc.DeveloperGuide.Internationalization.DynamicStringInternationalization.html">next &raquo;</a></nobr>
</div>
<span class="item"><a href="http://code.google.com/">Google Code Home</a></span>
        &gt;
        <span class="item"><a href="http://code.google.com/webtoolkit/">Google Web Toolkit</a></span>
        &gt;
        
        <span class="item"><a xmlns="" href="com.google.gwt.doc.DeveloperGuide.html">Developer Guide</a></span> &gt;
						<span class="item"><a href="com.google.gwt.doc.DeveloperGuide.Internationalization.html">Internationalization</a></span> &gt;
						<span class="selected item">Static String Internationalization</span>
</div>
</div>
<div id="side">
<div id="menu">
<h4>
<a href="http://code.google.com/webtoolkit/">Google Web Toolkit</a>
</h4>
<ul>
<li>
<a href="http://code.google.com/webtoolkit/download.html">Download GWT</a>
</li>
</ul>
<ul>
<li>
<a href="http://code.google.com/webtoolkit/overview.html">Product Overview</a>
</li>
<li>
<a href="http://code.google.com/webtoolkit/gettingstarted.html">Getting Started Guide</a>
</li>
<li>
<a href="http://code.google.com/webtoolkit/documentation/examples/">Example Projects</a>
</li>
</ul>
<ul>
<li>
<a class="selected" href="./com.google.gwt.doc.DeveloperGuide.html">Developer Guide</a>
</li>
<li>
<a href="./gwt.html">Class Reference</a>
</li>
<li>
<a href="http://code.google.com/webtoolkit/issues/">Issue Tracking</a>
</li>
<li>
<a href="http://groups.google.com/group/Google-Web-Toolkit">Developer Forum</a>
</li>
</ul>
<ul>
<li>
<a href="http://googlewebtoolkit.blogspot.com/">GWT Blog</a>
</li>
<li>
<a href="http://code.google.com/webtoolkit/faq.html">GWT FAQ</a>
</li>
<li>
<a href="http://code.google.com/webtoolkit/makinggwtbetter.html">Making GWT Better</a>
</li>
</ul>
<ul>
<li>
<a href="http://code.google.com/webtoolkit/thirdparty.html">Third Party Tools</a>
</li>
</ul>
</div>
<div id="search">
<form action="http://www.google.com/search" method="get">
<div>
<input name="domains" value="code.google.com" type="hidden"><input name="sitesearch" value="code.google.com" type="hidden">
<div class="header">Search this site:</div>
<div class="input">
<input name="q" size="10">
</div>
<div class="button">
<input value="Search" type="submit">
</div>
</div>
</form>
</div>
</div>
<div xmlns="http://www.w3.org/1999/xhtml" id="body">
<h1>Static String Internationalization</h1>Static string localization relies on code generation from
 <a xmlns="" href="com.google.gwt.doc.DeveloperGuide.Internationalization.PropertiesFiles.html">properties files</a>.
 GWT supports static string localization through two tag interfaces (that
 is, interfaces having no methods that represent a functionality contract)
 and a code generation library to generate implementations of those
 interfaces.
 
 <p>
 For example, if you wanted to localize the constant strings "hello,
 world" and "goodbye, world" in your GWT application, you could define an
 interface that abstracts those strings by extending the built-in
 <code>Constants</code> interface:
 
 <pre class="code">public interface MyConstants extends Constants {
  String helloWorld();
  String goodbyeWorld();
}
</pre>

 
 Now create an associated default properties file called
 <code>MyConstants.properties</code> in the same package:
 
 <pre class="code">helloWorld = hello, world
goodbyeWorld = goodbye, world</pre>

 
 You can also create a localized translation for each supported locale in
 separate properties file. In this case, we localize for Spanish:
 
 <pre class="code">helloWorld = hola, mundo
goodbyeWorld = adi&oacute;s, mundo
</pre>

 
 To use the internationalized constants, you create an implementation of
 <code>MyConstants</code> using <a href="com.google.gwt.core.client.GWT.html#create(java.lang.Class)">GWT.create(Class)</a>:
 
 <pre class="code">public void useMyConstants() {
  MyConstants myConstants = (MyConstants) GWT.create(MyConstants.class);
  Window.alert(myConstants.helloWorld());
}
</pre>
</p>
<h2>The Benefits of Static String Internationalization</h2>
 As you can see from the example above, static internationalization relies
 on a very tight binding between internationalized code and its localized
 resources. Using explicit method calls in this way has a number of
 advantages. The GWT compiler can optimize deeply, removing uncalled
 methods and inlining localized strings -- making generated code as
 efficient as if the strings had been hard-coded.
 
 <p>
 The value of compile-time checking becomes even more apparent when
 applied to messages that take multiple arguments. Creating a Java method
 for each message allows the compiler to check both the number and types
 of arguments supplied by the calling code against the message template
 defined in a properties file. For example, attempting to use this
 interface:
 
 <pre class="code">public interface ErrorMessages extends Messages {
  String permissionDenied(int errorCode, String username);
}
</pre>

 
 with this properties file:
 
 <pre class="code">permissionDenied = Error {0}: User {1} does not have permission to access {2} </pre>

 
 results in a compile-time error because the message template in the
 properties file expects three arguments, while the
 <code>permissionDenied</code> method can only supply two.
 </p>
<h2>Which Interface to Use?</h2>
 Extend <code><a href="com.google.gwt.i18n.client.Constants.html">Constants</a></code> to
 create a collection of constant values of a variety of types that can be
 accessed by calling methods (called <i>constant accessors</i>) on an
 interface. Constant accessors may return a variety of types, including
 strings, numbers, booleans, and even maps. A compile-time check is done
 to ensure that the value in a properties file matches the return type
 declared by its corresponding constant accessor. In other words, if a
 constant accessor is declared to return an <code>int</code>, its
 associated property is guaranteed to be a valid <code>int</code> value --
 avoiding a potential source of runtime errors.
 
 <p>
<code><a href="com.google.gwt.i18n.client.ConstantsWithLookup.html">ConstantsWithLookup</a></code> is
 identical to <code>Constants</code> except that the interface also
 includes a method to look up strings by property name, which facilitates
 dynamic binding to constants by name at runtime.
 <code>ConstantsWithLookup</code> can sometimes be useful in highly
 data-driven applications. One caveat: <code>ConstantsWithLookup</code>
 is less efficient than <code>Constants</code> because the compiler
 cannot discard unused constant methods, resulting in larger applications.
 </p>
<p>
 Extend <code><a href="com.google.gwt.i18n.client.Messages.html">Messages</a></code> to
 create a collection of formatted messages that can accept parameters. You
 might think of the <code>Messages</code> interface as a statically
 verifiable equivalent of the traditional Java combination of
 <code>Properties</code>, <code>ResourceBundle</code>, and
 <code>MessageFormat</code> rolled into a single mechanism.
 </p>
<h2>Properties Files</h2>
 All of the types above use properties files based on the traditional <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html#load(java.io.InputStream)">Java
 properties file format</a>, although GWT uses
 <a href="com.google.gwt.doc.DeveloperGuide.Internationalization.PropertiesFiles.html">an enhanced properties file format</a>
 that are encoded as UTF-8 and can therefore contain Unicode characters
 directly.<div class="topicSeeAlso" xmlns="http://www.w3.org/1999/xhtml">
<h2>Related topics</h2>
<a xmlns="" href="com.google.gwt.i18n.client.Constants.html">Constants</a>,<a href="com.google.gwt.i18n.client.ConstantsWithLookup.html">ConstantsWithLookup</a>,<a href="com.google.gwt.i18n.client.Messages.html">Messages</a>,<a href="com.google.gwt.doc.DeveloperGuide.Internationalization.PropertiesFiles.html">Localized Properties Files</a>,<a href="com.google.gwt.doc.DeveloperGuide.Internationalization.DynamicStringInternationalization.html">Dynamic String Internationalization</a>,<a href="com.google.gwt.i18n.client.Dictionary.html">Dictionary</a>
</div>
</div>
<div id="footer" xmlns="http://www.w3.org/1999/xhtml">
          &copy;2007 Google
          <span class="noprint">
            -
            <a href="http://www.google.com/">Google Home</a>
            -
            <a href="http://www.google.com/jobs/">We're Hiring</a>
            -
            <a href="http://www.google.com/privacy.html">Privacy Policy</a>
            -
            <a href="http://www.google.com/terms_of_service.html">Terms of Service</a>
            -
            <a href="mailto:code@google.com">Contact Us</a></span>
<div id="license" style="text-align: center; margin: 1em 0em 1em 0em">
            Except as otherwise
            <a href="http://code.google.com/policies.html#restrictions">noted</a>, the content of this  page is licensed under the  <a rel="license" href="http://creativecommons.org/licenses/by/2.5/">Creative Commons Attribution 2.5 License</a>.
              <!--
            <rdf:RDF xmlns="http://web.resource.org/cc/">
<Work rdf:about="">
<license rdf:resource="http://creativecommons.org/licenses/by/2.5/"></license>
</Work>
<License rdf:about="http://creativecommons.org/licenses/by/2.5/">
<permits rdf:resource="http://web.resource.org/cc/Reproduction"></permits>
<permits rdf:resource="http://web.resource.org/cc/Distribution"></permits>
<requires rdf:resource="http://web.resource.org/cc/Notice"></requires>
<requires rdf:resource="http://web.resource.org/cc/Attribution"></requires>
<permits rdf:resource="http://web.resource.org/cc/DerivativeWorks"></permits>
</License>
</rdf:RDF>
              -->
            </div>
</div>
<script src="https://ssl.google-analytics.com/urchin.js" type="text/javascript" xmlns="http://www.w3.org/1999/xhtml"></script><script type="text/javascript">
          _uacct="UA-18071-1"; _uanchor=1; urchinTracker();
        </script>
</body>
</html>
